package com.zcoj.controller.User.problem;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.ContestProblemMapper;
import com.zcoj.mapper.GroupProblemMapper;
import com.zcoj.mapper.TrainingProblemMapper;
import com.zcoj.pojo.Dto.ContestProblem;
import com.zcoj.pojo.Dto.GroupProblem;
import com.zcoj.pojo.Dto.Problem;
import com.zcoj.pojo.Dto.TrainingProblem;
import com.zcoj.pojo.Vo.ProblemSubmitCodeData;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.ProblemService;
import com.zcoj.service.ProblemVoService;
import com.zcoj.util.RedisCaches;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 接口
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@RestController
@RequestMapping("/problem")
@Api(tags = "接口")
public class ProblemController{

    @Autowired
    private ProblemService problemService;


    @Autowired
    private RedisCaches redisCaches;

    @Autowired
    private ContestProblemMapper contestProblemMapper;

    @Autowired
    private GroupProblemMapper groupProblemMapper;

    @Autowired
    private TrainingProblemMapper trainingProblemMapper;


    @Autowired
    private ProblemVoService problemVoService;
    /**
     * 创建
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/create")
    @ApiOperation("新建")
    public ApiResponse create(@RequestBody Problem problem) {
        return ApiResponse.success(problemService.create(problem));
    }

    /**
     * 删除
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    public ApiResponse deleteById(@PathVariable Integer id) {
        problemService.deleteById(id);
        return ApiResponse.success(null);
    }

    /**
     * 根据ID修改
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/updateById")
    @ApiOperation("根据ID修改")
    public ApiResponse updateById(@RequestBody Problem problem) {
        problemService.updateById(problem);
        return ApiResponse.success(null);
    }

    /**
     * 分页查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public ApiResponse findPage (@RequestBody PageWrap<Problem> pageWrap) {
        return ApiResponse.success(problemService.findPage(pageWrap));
    }

    @PostMapping("/pageVo")
    @ApiModelProperty("分页查询VO")
    public ApiResponse findPageVo(@RequestBody PageWrap<Problem> pageWrap){
        LoginUser loginUser = redisCaches.getLoginUser();
        if(!(loginUser.getPermission().contains("super_admin")||loginUser.getPermission().contains("admin")||loginUser.getPermission().contains("problem_admin"))){
            pageWrap.getModel().setStatus(1);
        }else {
            pageWrap.getModel().setStatus(null);
        }
        return ApiResponse.success(problemService.findPageVO(pageWrap));

    }



    /**
     * @method: [findComponentPageVO]
     * @param: [pageWrap]
     * @returns: com.zcoj.core.model.ApiResponse
     * @description: 根据用户角色查询列表
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/4/21 23:26
     **/
    @PostMapping("/findComponentPageVO")
    @ApiModelProperty("分页查询VO")
    public ApiResponse findComponentPageVO(@RequestBody PageWrap<Problem> pageWrap){
        return ApiResponse.success(problemVoService.findComponentPageVO(pageWrap));

    }
    /**
     * 通过ID查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
//    @GetMapping("/{id}/{permission}")
//    @ApiOperation("根据ID查询")
//    public ApiResponse finById(@PathVariable Integer id,@PathVariable String permission) {
//        LoginUser loginUser = redisCaches.getLoginUser();
//        Problem problemServiceById = problemService.findById(id);
//        if( Objects.nonNull(problemServiceById)){
//            if(problemServiceById.getStatus()==1){
//                return ApiResponse.success("",problemServiceById);
//            }else if(loginUser.getPermission().contains("problem_admin")||loginUser.getPermission().contains("super_admin")||loginUser.getPermission().contains("admin")){
//                return ApiResponse.success(problemServiceById);
//            }else {
//                return ApiResponse.failed("题目未公开");
//            }
//        }else {
//            return ApiResponse.failed("题目ID不存在");
//        }
//
//    }


    //getProblemData
    @PostMapping("/getProblemData")
    @ApiOperation("根据ID查询")
    public ApiResponse getProblemData(@RequestBody ProblemSubmitCodeData problemSubmitCodeData) {
        LoginUser loginUser = redisCaches.getLoginUser();
        Problem problemServiceById = problemService.findById(problemSubmitCodeData.getPid());
        if( Objects.nonNull(problemServiceById)){
            if(problemServiceById.getStatus()==1){
                return ApiResponse.success("",problemServiceById);
            }else if(loginUser.getPermission().contains("problem_admin")||loginUser.getPermission().contains("super_admin")||loginUser.getPermission().contains("admin")){
                return ApiResponse.success(problemServiceById);
            }else if(Objects.nonNull(problemSubmitCodeData.getCid())&&problemSubmitCodeData.getCid()!=0){
                QueryWrapper<ContestProblem> contestProblemQueryWrapper=new QueryWrapper<>();
                contestProblemQueryWrapper.eq("cid",problemSubmitCodeData.getCid()).eq("pid",problemSubmitCodeData.getPid());
                Integer selectCount = contestProblemMapper.selectCount(contestProblemQueryWrapper);
                if(selectCount!=0){
                    return ApiResponse.success(problemServiceById);
                }else {
                    return ApiResponse.failed("数据错误");
                }
            }else if(Objects.nonNull(problemSubmitCodeData.getGid())&&problemSubmitCodeData.getGid()!=0){
                QueryWrapper<GroupProblem> groupProblemQueryWrapper=new QueryWrapper<>();
                groupProblemQueryWrapper.eq("gid",problemSubmitCodeData.getGid()).eq("pid",problemSubmitCodeData.getPid());
                Integer selectCount = groupProblemMapper.selectCount(groupProblemQueryWrapper);
                if(selectCount!=0){
                    return ApiResponse.success(problemServiceById);
                }else {
                    return ApiResponse.failed("数据错误");
                }
            }else if(Objects.nonNull(problemSubmitCodeData.getTid())&&problemSubmitCodeData.getTid()!=0){
                QueryWrapper<TrainingProblem> trainingProblemQueryWrapper=new QueryWrapper<>();
                trainingProblemQueryWrapper.eq("tid",problemSubmitCodeData.getTid()).eq("pid",problemSubmitCodeData.getPid());
                Integer selectCount = trainingProblemMapper.selectCount(trainingProblemQueryWrapper);
                if(selectCount!=0){
                    return ApiResponse.success(problemServiceById);
                }else {
                    return ApiResponse.failed("数据错误");
                }
            }else{
                return ApiResponse.failed("题目未公开");
            }


        }else {
            return ApiResponse.failed("题目ID不存在");
        }

    }
    @GetMapping("/source")
    @ApiModelProperty("获取题目来源")
    public ApiResponse getProblemSource(){
        return problemService.getProblemSource();
    }

    @GetMapping("/tag")
    @ApiModelProperty("获取题目标签")
    public ApiResponse getProblemTag(){
        return problemService.getProblemTag();
    }

}
